package com.stn.zjx.givingtreasure.fragment.active;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.makeramen.roundedimageview.RoundedImageView;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.DynamicPrivateSelectListAdapter;
import com.stn.zjx.givingtreasure.adapter.DynamicPrivatetemListAdapter;
import com.stn.zjx.givingtreasure.base.BaseMainFragment;
import com.stn.zjx.givingtreasure.bean.DynamicPrivateSelectItem;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.utils.ToolsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.bgabanner.BGABanner;

public class PrivateFragment extends BaseMainFragment {
    private static final float MIN_SCALE = 0.85f;
    public View rootView;
    @BindView(R.id.banner_content)
    BGABanner bannerContent;
    @BindView(R.id.select_gv)
    RecyclerView selectGv;
    @BindView(R.id.rv)
    RecyclerView rv;


    private DynamicPrivateSelectListAdapter adapter;
    private LinearLayoutManager lm;
    private DynamicPrivatetemListAdapter dynamicAdapter;
    private LinearLayoutManager rv_lm;

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container) {
        if (rootView == null) {
            rootView = View.inflate(activity, R.layout.fragment_active_private, null);
            ButterKnife.bind(this, rootView);
            selectGv.setLayoutManager(lm = new LinearLayoutManager(activity, OrientationHelper.HORIZONTAL, false));
            selectGv.setAdapter(adapter = new DynamicPrivateSelectListAdapter(activity));
            rv.setLayoutManager(rv_lm=new LinearLayoutManager(activity,OrientationHelper.VERTICAL,false));
            rv.setAdapter(dynamicAdapter=new DynamicPrivatetemListAdapter(activity));
        }
        return rootView;
    }

    @Override
    public void initData() {


        List<String> stringList = new ArrayList<>();
        stringList.add("https://img.alicdn.com/tfs/TB10rtgpNTpK1RjSZR0XXbEwXXa-520-280.jpg_q90");
        stringList.add("https://img.alicdn.com/simba/img/TB1iOGzqCzqK1RjSZFLSuwn2XXa.jpg");
        stringList.add("https://img.alicdn.com/tps/i4/TB1fwhGpFzqK1RjSZFCSuvbxVXa.jpg_q90");
        initBanner(stringList);


        List<DynamicPrivateSelectItem> items = new ArrayList<>();
        items.add(new DynamicPrivateSelectItem(R.mipmap.dynamic_show, getString(R.string.show)));
        items.add(new DynamicPrivateSelectItem(R.mipmap.dynamic_health, getString(R.string.health)));
        items.add(new DynamicPrivateSelectItem(R.mipmap.dynamic_lecture, getString(R.string.lecture)));
        items.add(new DynamicPrivateSelectItem(R.mipmap.dynamic_cara_ok, getString(R.string.cara_ok)));
        items.add(new DynamicPrivateSelectItem(R.mipmap.dynamic_welfare, getString(R.string.welfare)));



        adapter.refreshList(items);

        List<String> strings = new ArrayList<>();
        strings.add("dasdas");
        strings.add("dsad");
        strings.add("dsadsa");
        strings.add("dsasda");
        strings.add("dsad");
        strings.add("dsadsa");
        strings.add("dsasda");
        strings.add("dsad");
        strings.add("dsadsa");
        strings.add("dsasda");
        strings.add("dsad");
        strings.add("dsadsa");
        strings.add("dsasda");
        strings.add("dsad");
        strings.add("dsadsa");
        strings.add("dsasda");

        dynamicAdapter.refreshList(strings);

    }

    public void initBanner(List<String> datas) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        bannerContent.setAdapter(new BGABanner.Adapter() {
            @Override
            public void fillBannerItem(BGABanner banner, View itemView, @Nullable Object model, int position) {
                System.out.println("*********************model:" + model);
            }
        });
        List<View> viewList = new ArrayList<>();
        View view = null;
        for (int i = 0; i < datas.size(); i++) {
            String data = datas.get(i);
            view = View.inflate(activity, R.layout.item_banner_layout, null);
            RoundedImageView iv = view.findViewById(R.id.iv);
            Glide.with(this)
                    .asBitmap()
                    .load(data)
                    .thumbnail(0.2f)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            iv.setImageBitmap(resource);
                        }
                    });
            viewList.add(view);
        }
        bannerContent.setData(viewList, datas, null);
        bannerContent.getViewPager().setPageMargin(ToolsUtils.dip2px(activity, 50) * -1);
        bannerContent.getViewPager().setPageTransformer(false, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(View view, float position) {
                if (viewList.get(0) == view) {
//                     System.out.println("aaaaaa:" + view.toString() + "-" + position);
                }
//
//                View imageView = view.findViewById(R.id.image);
                //float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                if (position < -1 || position > 1) {
                    view.setScaleX(1);
                    view.setScaleY(1);
                    return;
                }
                float changeArea = 1 - MIN_SCALE;
                position = Math.abs(position);
                float scaleFactor = position * changeArea;
                view.setScaleX(1 - scaleFactor);
                view.setScaleY(1 - scaleFactor);

            }

        });

    }


}
